package com.rainng.coursesystem.manager;

import com.rainng.coursesystem.dao.AdminDAO;
import com.rainng.coursesystem.dao.MemberDAO;
import com.rainng.coursesystem.dao.LeaderDAO;
import com.rainng.coursesystem.model.bo.AuthInfoBO;
import com.rainng.coursesystem.model.constant.UserType;
import com.rainng.coursesystem.model.entity.MemberEntity;
import org.springframework.stereotype.Component;

@Component
public class UserManager extends BaseManager {
    private final AdminDAO adminDAO;
    private final LeaderDAO leaderDAO;
    private final MemberDAO memberDAO;

    public UserManager(AdminDAO adminDAO, LeaderDAO leaderDAO, MemberDAO memberDAO) {
        this.adminDAO = adminDAO;
        this.leaderDAO = leaderDAO;
        this.memberDAO = memberDAO;
    }

    public AuthInfoBO getAuthInfoByUsername(String username, Integer userType) {
        if (userType == UserType.MEMBER) {
            return AuthInfoBO.fromMember(memberDAO.getByNumber(username));
        } else if (userType == UserType.LEADER) {
            return AuthInfoBO.fromLeader(leaderDAO.getByNumber(username));
        } else if (userType == UserType.ADMIN) {
            return AuthInfoBO.fromAdmin(adminDAO.getByUsername(username));
        }

        return null;
    }

    public void updateMemberLastLoginTime(String number) {
        MemberEntity entity = memberDAO.getByNumber(number);
        if (entity == null) {
            return;
        }

        //entity.setLastLoginTime(new Date());
        memberDAO.update(entity);
    }
}
